<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<script>
    //封装链表类
    function LinkedList() {
           //内部节点类
        function Node (data){
            this.data = data;
            this.next = null
        }

        this.head = null;
        //记录链表的长度
        this.length = 0;
//  添加元素
        LinkedList.prototype.enNode = function(element){
            var newNode = new Node(element);
            if(!this.head){
                this.head = newNode;
            }else{
                var current = this.head;
                while(current.next){
                    current = current.next;
                }
                current.next = newNode;
            }
            this.length++;
        };
        //insert()
        LinkedList.prototype.insert = function(position,data){
            //position的边界检测
            var newNode = new Node(data);
            if(position<0 || position>this.length){
                return null;
            }
            var current = this.head;
            var index = 0;
            if(position === 0){
                newNode.next = current;
                this.head = newNode;
                this.length++;
            }else{
                while(index<position-1 ){
                    current = current.next;
                    index++;
                }
                newNode.next = current.next;
                current.next = newNode;
                this.length++;

            }

        };

        //get(position)
        //使用linkedlist中的属性的时候一定要添加this，this指向的是当前实例
        LinkedList.prototype.get = function (position){
            if(position < 0|| position >=this.length){

                return null;
            }

            //get data
            var index = 0;
            var current = this.head;


            while(index<position){
                current = current.next;
                index++;
            }

            return current.data;

        };

        //indexOf 返回数据的对应位置,如果找不到，则返回-1
        LinkedList.prototype.indexOf = function (data){

            var current = this.head;
            var index = 0;
            while(current){
                if(current.data!==data){
                    current = current.next;
                    index++
                }else{
                    return index;
                }
            }
            return -1;
        };
        //update 根据位置信息修改数据
        LinkedList.prototype.upDate = function(position,newData){
            //判断position是否越界
            var index = 0;
            var current = this.head;
            if(position<0||position >= this.length){
                return false;
            }
            //查找修改位置
            while(index < position){
                current = current.next;
                index++;
            }
            //修改数据
            current.data = newData;
            return true;

        };
        //removeAt 删除指定位置的数据
        LinkedList.prototype.removeAt = function(position){
            //判断position是否越界
            if(position<0||position>=this.length){
                return null;
            }
            //查找删除的位置
                //position == 0
                //position == this.length - 1
            var index = 0;
            var current = this.head,
                previous = null;

            if(position === 0){
                this.head = this.head.next;
            }else{
                while(index < position){
                    previous = current;
                    current = current.next;
                    index++;
                }
                previous.next = current.next;

            }
            //删除
            this.length -=1;
            return current;


        };
        //remove 根据data删除元素
        LinkedList.prototype.remove = function(data){

            var position = this.indexOf(data);
            return this.removeAt(position);

        };


        //isEmpty()
        LinkedList.prototype.isEmpty = function(){
            return this.length === 0;
        };

        //size()
        LinkedList.prototype.size = function(){
            return this.length;
        };
        //toString 方法
        LinkedList.prototype.toString = function(){
            var resultString = "";
            if(!this.head){
                return "";
            }else{
                var current = this.head;
                while(current){
                    resultString += current.data + " ";
                    current = current.next;
                }

            }
            return resultString;
        };

        //

    }

//测试
    //
    var list = new LinkedList();
    list.enNode(123);
    list.enNode("qwe");
    list.enNode("df");
    list.enNode("gfd");
    list.enNode("cdgf");
    list.insert(3,"aaa");
    console.log(list.get(0));

    console.log(list);
    console.log(list.toString());
    console.log(list.indexOf(123));
    console.log(list.upDate(0, 321));
    // console.log(list.removeAt(0));
    console.log(list.remove(321));
    console.log(list.toString());


</script>
</body>
</html>